# -*- coding:utf-8 -*-
import web, service, models

class BaseHandler:
    @classmethod
    def logined(self):
        return True if web.ctx.session.get('login') else False

    def login_required(self, username = None):
        if (not web.ctx.session.get('login')) or (username and username != web.ctx.session.get('username')):
            raise web.seeother(r'/login')

    def admin_requried(self):
        user = self.getCurrentUser()
        if models.User.TYPE_ADMIN != user.type:
            raise service.ServiceException.ServiceException(service.UNAUTHORIZED, '没有权限')
        return user

    def chief_requried(self):
        user = self.getCurrentUser()
        if models.User.TYPE_CHIEF != user.type:
            raise service.ServiceException.ServiceException(service.UNAUTHORIZED, '没有权限')
        return user

    def getCurrentUser(self):
        self.login_required()
        return web.ctx.session.user